package top.infopub.mgr.bpa.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.mgr.bpa.dao.EffectPlanDao;
import top.infopub.mgr.bpa.dao.EffectPlanDetailDao;
import top.infopub.mgr.bpa.dao.ProContractDao;
import top.infopub.mgr.bpa.domain.EffectPlanDomain;
import top.infopub.mgr.bpa.service.EffectPlanService;
import top.infopub.mgr.bpa.vo.EffectPlanVo;
import top.infopub.mgr.flow.cnst.FlowTemplateEnum;
import top.infopub.mgr.flow.service.WsFlowProcessService;
import top.infopub.mgr.hr.dao.OrgInfoDao;
import top.infopub.model.bpa.EffectPlan;
import top.infopub.model.bpa.EffectPlanDetail;

import java.util.List;

@Service
public class EffectPlanServiceImpl extends GenericServiceImpl<EffectPlan, String> implements
		EffectPlanService {

	@Autowired
	private EffectPlanDao effectPlanDao;

	@Autowired
	private WsFlowProcessService wsFlowProcess;

	@Autowired
	private EffectPlanDetailDao effectPlanDetailDao;

	@Autowired
	private ProContractDao proContractDao;

	@Autowired
	private OrgInfoDao orgInfoDao;
	
	@Override
	public GenericDao<EffectPlan, String> getDao() {
		return effectPlanDao;
	}

	@Override
	public Page<EffectPlanDomain> selectPageByDomainWhere(Page<EffectPlanDomain> page, EffectPlanVo effectPlanVo) {
		PageHelper.startPage(page.getPageNum(), page.getPageSize());
		return (Page<EffectPlanDomain>)effectPlanDao.selectByDomainWhere(effectPlanVo);
	}

	@Override
	public Page<EffectPlanDomain> selectMyCheckByDomainWhere(Page<EffectPlanDomain> page, EffectPlanVo effectPlanVo) {
		PageHelper.startPage(page.getPageNum(), page.getPageSize());
		return (Page<EffectPlanDomain>)effectPlanDao.selectMyCheckByDomainWhere(effectPlanVo);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void save(EffectPlan effectPlan, List<EffectPlanDetail> detailList) {
		//插入预立项记录
		effectPlanDao.insert(effectPlan);

		//批量插入子项
		if(CollectionUtils.isNotEmpty(detailList)){
			effectPlanDetailDao.batchInsert(detailList);
		}
		// 注意 这里根据不同的业务分支判断条件 获得不同enum
		FlowTemplateEnum templateEnum = FlowTemplateEnum.BPA_EFFECT_PLAN;
		// 处理工作流数据
		wsFlowProcess.launch(templateEnum, effectPlan.getId(), effectPlan.getProcessId(), null);
	}

	@Override
	public EffectPlanVo queryById(String id) {
	    return effectPlanDao.queryById(id);
		/*EffectPlanVo effectPlanVo = effectPlanDao.queryById(id);
		 // 项目公司名，区域公司名处理
        if (StringUtils.isNotBlank(effectPlanVo.getHrOrgProId())) {
            effectPlanVo.setOrgProName(orgInfoDao.selectByPrimaryKey(effectPlanVo.getHrOrgProId()).getCompName());
        }
        if (StringUtils.isNotBlank(effectPlanVo.getHrOrgAreaId())) {
            effectPlanVo.setOrgAreName(orgInfoDao.selectByPrimaryKey(effectPlanVo.getHrOrgAreaId()).getCompName());
        }
		return effectPlanVo;*/
	}

    @Override
    public EffectPlan selectLastedEffectPlan(EffectPlan effectPlan) {
        return effectPlanDao.selectLastedEffectPlan(effectPlan);
    }
}